package jnpf.controller;

import io.swagger.annotations.Api;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.util.DynDicUtil;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.config.PermissionsConfig;
import jnpf.entity.ZXGL_XiangMuLiXiang_MingXiEntity;
import jnpf.exception.DataException;
import jnpf.generater.util.GeneraterSwapUtil;
import jnpf.model.zxgl_xiangmulixiang_mingxi.*;
import jnpf.permission.OrganizeApi;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.service.ZXGL_XiangMuFenJie_mingxibiaoService;
import jnpf.service.ZXGL_XiangMuLiXiang_MingXiService;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * ZXGL_XiangMuLiXiang_MingXi
 *
 * @版本： V2.0
 * @版权： 河北交投智能交通技术有限责任公司
 * @作者： 数字交投项目组
 * @日期： 2025-03-20
 */
@Slf4j
@RestController
@Api(tags = "ZXGL_XiangMuLiXiang_MingXi", value = "zxgl")
@RequestMapping("/ZXGL_XiangMuLiXiang_MingXi")
public class ZXGL_XiangMuLiXiang_MingXiController {


    @Autowired
    private DynDicUtil dynDicUtil;

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private OrganizeApi organizeApi;

    @Autowired
    private ZXGL_XiangMuLiXiang_MingXiService zXGL_XiangMuLiXiang_MingXiService;

    @Autowired
    private ZXGL_XiangMuFenJie_mingxibiaoService zXGL_XiangMuFenJie_mingxibiaoService;

    @Resource
    PermissionsConfig permissionsConfig;

    /**
     * 列表
     *
     * @param zXGL_XiangMuLiXiang_MingXiPagination
     * @return
     */
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZXGL_XiangMuLiXiang_MingXiPagination zXGL_XiangMuLiXiang_MingXiPagination) throws IOException {
        List<ZXGL_XiangMuLiXiang_MingXiEntity> list = zXGL_XiangMuLiXiang_MingXiService.getList(zXGL_XiangMuLiXiang_MingXiPagination);
        //处理id字段转名称，若无需转或者为空可删除
//        for (ZXGL_XiangMuLiXiang_MingXiEntity entity : list) {
//            entity.setXmlx(dynDicUtil.getDicName(entity.getXmlx(), "667277254828496197"));
//            entity.setXmly(dynDicUtil.getDicName(entity.getXmly(), "667353988936504645"));
//            entity.setZzbm(generaterSwapUtil.comSelectValues(entity.getZzbm()));
//            entity.setZzr(generaterSwapUtil.userSelectValues(entity.getZzr()));
//            entity.setXzr(generaterSwapUtil.userSelectValues(entity.getXzr()));
//            entity.setXzbm(generaterSwapUtil.comSelectValues(entity.getXzbm()));
//            entity.setXzdw(generaterSwapUtil.comSelectValues(entity.getXzdw()));
//            entity.setJszt(dynDicUtil.getDicName(entity.getJszt(), "669882974119949957"));
//            entity.setZt(dynDicUtil.getDicName(entity.getZt(), "667276146122954053"));
//            entity.setXmwj(generaterSwapUtil.getFileNameInJson(entity.getXmwj()));
//            entity.setYjbj(dynDicUtil.getDicName(entity.getYjbj(), "667375644950140229"));
//            entity.setJxjfw(generaterSwapUtil.getFileNameInJson(entity.getJxjfw()));
//            entity.setCkqxkz(generaterSwapUtil.userSelectValues(entity.getCkqxkz()));
//            entity.setSjly(dynDicUtil.getDicName(entity.getSjly(), "669875872030815877"));
//            entity.setFkpl(dynDicUtil.getDicName(entity.getFkpl(), "667375897669539141"));
//            entity.setMblx(dynDicUtil.getDicName(entity.getMblx(), "670160918163781253"));
//            entity.setZdxm(dynDicUtil.getDicName(entity.getZdxm(), "669876395320571525"));
//        }
        boolean isAllDataRole = false;
        UserInfo userInfo = userProvider.get();
        if (userInfo.getIsAdministrator()) {
            isAllDataRole = true;
        } else {
            //全部数据权限的角色id
            String alldataRoleId = permissionsConfig.getAlldataRoleid();
            List<String> roleIds = userInfo.getRoleIds();
            if (roleIds != null && StringUtils.isNotBlank(alldataRoleId) && roleIds.contains(alldataRoleId)) {
                isAllDataRole = true;
            }
        }
        List<ZXGL_XiangMuLiXiang_MingXiListVO> listVO = JsonUtil.getJsonToList(list, ZXGL_XiangMuLiXiang_MingXiListVO.class);
        for (ZXGL_XiangMuLiXiang_MingXiListVO zXGL_XiangMuLiXiang_MingXiVO : listVO) {
            zXGL_XiangMuLiXiang_MingXiVO.setXmlx(dynDicUtil.getDicName(zXGL_XiangMuLiXiang_MingXiVO.getXmlx(), "667277254828496197"));
            zXGL_XiangMuLiXiang_MingXiVO.setXmly(dynDicUtil.getDicName(zXGL_XiangMuLiXiang_MingXiVO.getXmly(), "667353988936504645"));
            zXGL_XiangMuLiXiang_MingXiVO.setZzbm(generaterSwapUtil.comSelectValues(zXGL_XiangMuLiXiang_MingXiVO.getZzbm()));
            zXGL_XiangMuLiXiang_MingXiVO.setZzrid(zXGL_XiangMuLiXiang_MingXiVO.getZzr());
            zXGL_XiangMuLiXiang_MingXiVO.setZzr(generaterSwapUtil.userSelectValues(zXGL_XiangMuLiXiang_MingXiVO.getZzr()));
            zXGL_XiangMuLiXiang_MingXiVO.setXzrid(zXGL_XiangMuLiXiang_MingXiVO.getXzr());
            zXGL_XiangMuLiXiang_MingXiVO.setXzr(generaterSwapUtil.userSelectValues(zXGL_XiangMuLiXiang_MingXiVO.getXzr()));
            zXGL_XiangMuLiXiang_MingXiVO.setXzbm(generaterSwapUtil.comSelectValues(zXGL_XiangMuLiXiang_MingXiVO.getXzbm()));
            zXGL_XiangMuLiXiang_MingXiVO.setXzdw(generaterSwapUtil.comSelectValues(zXGL_XiangMuLiXiang_MingXiVO.getXzdw()));
            zXGL_XiangMuLiXiang_MingXiVO.setJszt(dynDicUtil.getDicName(zXGL_XiangMuLiXiang_MingXiVO.getJszt(), "669882974119949957"));
            zXGL_XiangMuLiXiang_MingXiVO.setZtmc(dynDicUtil.getDicName(zXGL_XiangMuLiXiang_MingXiVO.getZt(), "667276146122954053"));
            zXGL_XiangMuLiXiang_MingXiVO.setXmwj(generaterSwapUtil.getFileNameInJson(zXGL_XiangMuLiXiang_MingXiVO.getXmwj()));
            zXGL_XiangMuLiXiang_MingXiVO.setYjbj(dynDicUtil.getDicName(zXGL_XiangMuLiXiang_MingXiVO.getYjbj(), "667375644950140229"));
            zXGL_XiangMuLiXiang_MingXiVO.setJxjfw(generaterSwapUtil.getFileNameInJson(zXGL_XiangMuLiXiang_MingXiVO.getJxjfw()));
            zXGL_XiangMuLiXiang_MingXiVO.setCkqxkz(generaterSwapUtil.userSelectValues(zXGL_XiangMuLiXiang_MingXiVO.getCkqxkz()));
            zXGL_XiangMuLiXiang_MingXiVO.setSjly(dynDicUtil.getDicName(zXGL_XiangMuLiXiang_MingXiVO.getSjly(), "669875872030815877"));
            // zXGL_XiangMuLiXiang_MingXiVO.setFkpl(dynDicUtil.getDicName(zXGL_XiangMuLiXiang_MingXiVO.getFkpl(), "667375897669539141"));
            zXGL_XiangMuLiXiang_MingXiVO.setMblx(dynDicUtil.getDicName(zXGL_XiangMuLiXiang_MingXiVO.getMblx(), "670160918163781253"));
            zXGL_XiangMuLiXiang_MingXiVO.setZdxm(dynDicUtil.getDicName(zXGL_XiangMuLiXiang_MingXiVO.getZdxm(), "669876395320571525"));
            zXGL_XiangMuLiXiang_MingXiVO.setDepartmentManager(zXGL_XiangMuLiXiang_MingXiVO.getDepartmentManager());
            zXGL_XiangMuLiXiang_MingXiVO.setDepartmentManagerZzbm(zXGL_XiangMuLiXiang_MingXiVO.getDepartmentManagerZzbm());
            zXGL_XiangMuLiXiang_MingXiVO.setDepartmentManagerXzbm(zXGL_XiangMuLiXiang_MingXiVO.getDepartmentManagerXzbm());
            zXGL_XiangMuLiXiang_MingXiVO.setDepartmentHeadleader(zXGL_XiangMuLiXiang_MingXiVO.getDepartmentHeadleader());
            zXGL_XiangMuLiXiang_MingXiVO.setDepartmentHeadleaderZzbm(zXGL_XiangMuLiXiang_MingXiVO.getDepartmentHeadleaderZzbm());
            zXGL_XiangMuLiXiang_MingXiVO.setDepartmentHeadleaderXzbm(zXGL_XiangMuLiXiang_MingXiVO.getDepartmentHeadleaderXzbm());
            zXGL_XiangMuLiXiang_MingXiVO.setIsalldatarole(isAllDataRole);
        }


        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zXGL_XiangMuLiXiang_MingXiPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }


    /**
     * 创建
     *
     * @param zXGL_XiangMuLiXiang_MingXiCrForm
     * @return
     */
    @PostMapping
    @Transactional
    public ActionResult create(@RequestBody @Valid ZXGL_XiangMuLiXiang_MingXiCrForm zXGL_XiangMuLiXiang_MingXiCrForm) throws DataException {
        String mainId = RandomUtil.uuId();
        UserInfo userInfo = userProvider.get();
        ZXGL_XiangMuLiXiang_MingXiEntity entity = JsonUtil.getJsonToBean(zXGL_XiangMuLiXiang_MingXiCrForm, ZXGL_XiangMuLiXiang_MingXiEntity.class);
        entity.setId(mainId);
        entity.setCreatorTime(new Date());
        zXGL_XiangMuLiXiang_MingXiService.save(entity);


        return ActionResult.success("创建成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ActionResult<ZXGL_XiangMuLiXiang_MingXiInfoVO> info(@PathVariable("id") String id) {
        ZXGL_XiangMuLiXiang_MingXiEntity entity = zXGL_XiangMuLiXiang_MingXiService.getInfo(id);
        ZXGL_XiangMuLiXiang_MingXiInfoVO vo = JsonUtil.getJsonToBean(entity, ZXGL_XiangMuLiXiang_MingXiInfoVO.class);
        //子表
        //副表
        /**
         * 增加主责人部门负责人
         */
        if (StringUtil.isNotEmpty(vo.getZzbm())) {
            OrganizeEntity zbfzr = organizeApi.getById(vo.getZzbm());
            vo.setZzrbmfzr(zbfzr.getDirector());
        }
        return ActionResult.success(vo);
    }

    /**
     * 表单信息(详情页)
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public ActionResult<ZXGL_XiangMuLiXiang_MingXiInfoVO> detailInfo(@PathVariable("id") String id) {
        ZXGL_XiangMuLiXiang_MingXiEntity entity = zXGL_XiangMuLiXiang_MingXiService.getInfo(id);
        ZXGL_XiangMuLiXiang_MingXiInfoVO vo = JsonUtil.getJsonToBean(entity, ZXGL_XiangMuLiXiang_MingXiInfoVO.class);
        vo.setXmlx(dynDicUtil.getDicName(vo.getXmlx(), "667277254828496197"));
        vo.setXmly(dynDicUtil.getDicName(vo.getXmly(), "667353988936504645"));
        vo.setZzbm(generaterSwapUtil.comSelectValues(vo.getZzbm()));
        vo.setZzr(generaterSwapUtil.userSelectValues(vo.getZzr()));
        vo.setSpjd(dynDicUtil.getDicName(vo.getSpjd(), "667276146122954053"));
        vo.setXzr(generaterSwapUtil.userSelectValues(vo.getXzr()));
        vo.setXzbm(generaterSwapUtil.comSelectValues(vo.getXzbm()));
        vo.setXzdw(generaterSwapUtil.comSelectValues(vo.getXzdw()));
        vo.setJszt(dynDicUtil.getDicName(vo.getJszt(), "669882974119949957"));
        vo.setZt(dynDicUtil.getDicName(vo.getZt(), "667276146122954053"));
        vo.setXmwj(generaterSwapUtil.getFileNameInJson(vo.getXmwj()));
        vo.setYjbj(dynDicUtil.getDicName(vo.getYjbj(), "667375644950140229"));
        vo.setJxjfw(generaterSwapUtil.getFileNameInJson(vo.getJxjfw()));
        vo.setCkqxkz(generaterSwapUtil.userSelectValues(vo.getCkqxkz()));
        vo.setSjly(dynDicUtil.getDicName(vo.getSjly(), "669875872030815877"));
        vo.setFkpl(dynDicUtil.getDicName(vo.getFkpl(), "667375897669539141"));
        vo.setMblx(dynDicUtil.getDicName(vo.getMblx(), "670160918163781253"));
        vo.setZdxm(dynDicUtil.getDicName(vo.getZdxm(), "669876395320571525"));
        vo.setMbzdw(dynDicUtil.getDicName(vo.getMbzdw(), "671337925434177157"));
        vo.setFkzq(dynDicUtil.getDicName(vo.getFkzq(), "667375897669539141"));
        //子表数据转换

        //附表数据转换
        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZXGL_XiangMuLiXiang_MingXiUpForm zXGL_XiangMuLiXiang_MingXiUpForm) throws DataException {
        UserInfo userInfo = userProvider.get();
        ZXGL_XiangMuLiXiang_MingXiEntity entity = zXGL_XiangMuLiXiang_MingXiService.getInfo(id);
        if (entity != null) {
            ZXGL_XiangMuLiXiang_MingXiEntity subentity = JsonUtil.getJsonToBean(zXGL_XiangMuLiXiang_MingXiUpForm, ZXGL_XiangMuLiXiang_MingXiEntity.class);
            zXGL_XiangMuLiXiang_MingXiService.update(id, subentity);
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /**
     * 更新
     *
     * @param id
     * @return
     */
    @PutMapping("/updateDskg/{id}")
    @Transactional
    public ActionResult updateDskg(@PathVariable("id") String id, @RequestBody @Valid ZXGL_XiangMuLiXiang_MingXi_DskgUpForm zXGL_XiangMuLiXiang_MingXi_DskgUpForm) throws DataException {
        UserInfo userInfo = userProvider.get();
        ZXGL_XiangMuLiXiang_MingXiEntity entity = zXGL_XiangMuLiXiang_MingXiService.getInfo(id);
        if (entity != null) {
            entity.setDskg(zXGL_XiangMuLiXiang_MingXi_DskgUpForm.getDskg());
            entity.setFkpl(zXGL_XiangMuLiXiang_MingXi_DskgUpForm.getFkpl());
            zXGL_XiangMuLiXiang_MingXiService.update(id, entity);
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZXGL_XiangMuLiXiang_MingXiEntity entity = zXGL_XiangMuLiXiang_MingXiService.getInfo(id);
        if (entity != null) {
            zXGL_XiangMuLiXiang_MingXiService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }

    @GetMapping("/getXmliXiangById/{id}")
    public ActionResult getXmliXiangById(@PathVariable("id") String id) {
        return ActionResult.success(zXGL_XiangMuLiXiang_MingXiService.getXmliXiangById(id));
    }


    /**
     * 列表
     *
     * @param zXGL_XiangMuLiXiang_MingXiQuery
     * @return
     */
    @PostMapping("/listAll")
    public ActionResult listAll(@RequestBody ZXGL_XiangMuLiXiang_MingXiQuery zXGL_XiangMuLiXiang_MingXiQuery) throws IOException {
        //List<ZXGL_XiangMuLiXiang_MingXiEntity> list = zXGL_XiangMuLiXiang_MingXiService.getList(zXGL_XiangMuLiXiang_MingXiQuery);

        List<ZXGL_XiangMuLiXiang_MingXiEntity> list = zXGL_XiangMuLiXiang_MingXiService.selectAll(zXGL_XiangMuLiXiang_MingXiQuery.getXmmc(), userProvider.get().getUserId());
        //查询list中在表
        for (ZXGL_XiangMuLiXiang_MingXiEntity entity : list) {
            entity.setXmlx(dynDicUtil.getDicName(entity.getXmlx(), "667277254828496197"));
            entity.setXmly(dynDicUtil.getDicName(entity.getXmly(), "667353988936504645"));
            entity.setZzbm(generaterSwapUtil.comSelectValues(entity.getZzbm()));
            entity.setZzr(generaterSwapUtil.userSelectValues(entity.getZzr()));
            entity.setXzr(generaterSwapUtil.userSelectValues(entity.getXzr()));
            entity.setXzbm(generaterSwapUtil.comSelectValues(entity.getXzbm()));
            entity.setXzdw(generaterSwapUtil.comSelectValues(entity.getXzdw()));
            entity.setJszt(dynDicUtil.getDicName(entity.getJszt(), "669882974119949957"));
            entity.setZt(dynDicUtil.getDicName(entity.getZt(), "667276146122954053"));
            entity.setXmwj(generaterSwapUtil.getFileNameInJson(entity.getXmwj()));
            entity.setYjbj(dynDicUtil.getDicName(entity.getYjbj(), "667375644950140229"));
            entity.setJxjfw(generaterSwapUtil.getFileNameInJson(entity.getJxjfw()));
            entity.setCkqxkz(generaterSwapUtil.userSelectValues(entity.getCkqxkz()));
            entity.setSjly(dynDicUtil.getDicName(entity.getSjly(), "669875872030815877"));
            entity.setFkpl(dynDicUtil.getDicName(entity.getFkpl(), "667375897669539141"));
            entity.setMblx(dynDicUtil.getDicName(entity.getMblx(), "670160918163781253"));
            entity.setZdxm(dynDicUtil.getDicName(entity.getZdxm(), "669876395320571525"));
        }
        List<ZXGL_XiangMuLiXiang_MingXiListVO> listVO = JsonUtil.getJsonToList(list, ZXGL_XiangMuLiXiang_MingXiListVO.class);
        for (ZXGL_XiangMuLiXiang_MingXiListVO zXGL_XiangMuLiXiang_MingXiVO : listVO) {
        }

        return ActionResult.success(listVO);
    }

    /**
     * 列表
     *
     * @param zXGL_XiangMuLiXiang_MingXiPagination
     * @return
     */
    @PostMapping("/getListTree")
    public ActionResult getListTree(@RequestBody ZXGL_XiangMuLiXiang_MingXiPagination zXGL_XiangMuLiXiang_MingXiPagination) throws IOException {
        List<ZXGL_XiangMuLiXiang_MingXiEntity> list = zXGL_XiangMuLiXiang_MingXiService.getListTree(zXGL_XiangMuLiXiang_MingXiPagination);
        //处理id字段转名称，若无需转或者为空可删除
        for (ZXGL_XiangMuLiXiang_MingXiEntity entity : list) {
            entity.setXmlx(dynDicUtil.getDicName(entity.getXmlx(), "667277254828496197"));
            entity.setXmly(dynDicUtil.getDicName(entity.getXmly(), "667353988936504645"));
            entity.setZzbm(generaterSwapUtil.comSelectValues(entity.getZzbm()));
            entity.setZzr(generaterSwapUtil.userSelectValues(entity.getZzr()));
            entity.setXzr(generaterSwapUtil.userSelectValues(entity.getXzr()));
            entity.setXzbm(generaterSwapUtil.comSelectValues(entity.getXzbm()));
            entity.setXzdw(generaterSwapUtil.comSelectValues(entity.getXzdw()));
            entity.setJszt(dynDicUtil.getDicName(entity.getJszt(), "669882974119949957"));
            entity.setZt(dynDicUtil.getDicName(entity.getZt(), "667276146122954053"));
            entity.setXmwj(generaterSwapUtil.getFileNameInJson(entity.getXmwj()));
            entity.setYjbj(dynDicUtil.getDicName(entity.getYjbj(), "667375644950140229"));
            entity.setJxjfw(generaterSwapUtil.getFileNameInJson(entity.getJxjfw()));
            entity.setCkqxkz(generaterSwapUtil.userSelectValues(entity.getCkqxkz()));
            entity.setSjly(dynDicUtil.getDicName(entity.getSjly(), "669875872030815877"));
            entity.setFkpl(dynDicUtil.getDicName(entity.getFkpl(), "667375897669539141"));
            entity.setMblx(dynDicUtil.getDicName(entity.getMblx(), "670160918163781253"));
            entity.setZdxm(dynDicUtil.getDicName(entity.getZdxm(), "669876395320571525"));
        }
        List<ZXGL_XiangMuLiXiang_MingXiListVO> listVO = JsonUtil.getJsonToList(list, ZXGL_XiangMuLiXiang_MingXiListVO.class);
        for (ZXGL_XiangMuLiXiang_MingXiListVO zXGL_XiangMuLiXiang_MingXiVO : listVO) {
        }


        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zXGL_XiangMuLiXiang_MingXiPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    @GetMapping("/getBmByzrr/{zrr}")
    private String getBmByzrr(@PathVariable("zrr") String zrr) {
        log.info(zrr);
        log.info("获取部门");
        UserInfo userInfo = userProvider.get(zrr);
        return userInfo.getDepartmentId();
    }


    /**
     * 列表
     *
     * @param zxglFilePagination
     * @return
     */
    @PostMapping("/getFileList")
    public ActionResult getFileList(@RequestBody ZXGL_FilePagination zxglFilePagination) throws IOException {
        List<ZXGL_FileListVO> listVO = zXGL_XiangMuLiXiang_MingXiService.getList(zxglFilePagination);

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zxglFilePagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }


}
